<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<title>业务处理-采购申请-采购申请单</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
</head>
<body>
	<div id="mainContent">
		<div id="mainContent_content">
			<div class="page_header" >
			<a href="<%=basePath %>purchaseRequest/listPurchaseRequest.do">采购申请</a>
			<i class="icon-double-right"></i><a>采购申请单</a>
			</div>
			<div class="btn rightBtn">
				<input type="button" class="btn btn1" value="新增物品" onclick="toAddPurchaseRequestItem();"></input>
				<input type="button" class="btn btn2" value="保存" onclick="saveAddPurchaseRequest();"></input>
				<button class="btn btn1" style="margin-right: 10px;" id="submitOrder" >Excel导入</button> 
				<input type="button" class="btn btn2" value="返回" onclick="location.href='<%=basePath %>purchaseRequest/listPurchaseRequest.do'"></input>
				<form id="submitOrderForm" method="post" enctype="multipart/form-data" >
                	<input type="file" style="display: none;" id="submitOrderFile" name="submitOrder_files"						
                	accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
            	</form> 
			</div>
			<form id="purchaseRequestDetailForm" class="form">
			<div class="box">
				<p>
					<label>申请单号</label>
					<input type="text" class="form-control" readonly placeholder="自动生成"></input>
					<label>申请人</label>
					<input type="text" class="form-control" readonly value="${userName }"></input>
					<label>部门</label>
					<select name="deptID" class="form-control select">
						<option value=""></option>
						<c:forEach items="${organizations }" var="org">
							<option value="${org.organizationid }">${org.organizationname }</option>
						</c:forEach>
					</select>
					<label>邮轮</label>
					<select name="cruiseID" class="form-control select">
						<option value=""></option>
						<c:forEach items="${cruiseList }" var="cruise">
							<option value="${cruise.cruiseID }">${cruise.cruiseName }</option>
						</c:forEach>
					</select>
				</p>
				<p>
					<label>状态</label>
					<select name="status" class="form-control select">
						<option value="0">未提交</option>
						<option value="1">已提交</option>
					</select>
					<label>仓库</label>
					<select name="warehouseManagementID" class="form-control select">
						<c:forEach items="${warehouseList }" var="warehouse">
							<option value="${warehouse.warehousemanagementid }">${warehouse.warehousename }</option>
						</c:forEach>
					</select>
					<label>说明</label>
					<input type="text" class="form-control doubleWidth" name="explains"></input>
				</p>
				<p>
					<label>备注</label>
					<input type="text" class="form-control maxWidth" name="remarks"></input>
				</p>
			</div>
			<div class="tableBox box">
				<table class="table table-bordered table_text_center">
					<thead>
						<tr>
							<th>代码</th>
							<th>物品名称</th>
							<th>物品条码</th>
							<th>品牌</th>
							<th>库存</th>
							<th>单位</th>
							<th>采购数量</th>
							<th>小计</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="purchaseRequestItemTbody">
					</tbody>
				</table>
<!-- 				<div class="btn">
					<input type="button" value="编辑"></input>
					<input type="button" value="导出模板"></input>
					<input type="button" value="导入物品"></input>
				</div> -->
			</div>
			</form>
		</div>
		<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
		<jsp:include page="/jsp/business/purchaseRequest/addPurchaseRequestItem.jsp"></jsp:include>
	</div>
	<script src="<%=basePath %>js/jquery-3.2.1.min.js"></script>
	<script src="<%=basePath %>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="<%=basePath %>My97DatePickers/WdatePicker.js"></script>
	<script src="<%=basePath %>js/public.js"></script>
	<script type="text/javascript">
	function addToHasAddTable(item){
		var html="<tr>";
		html+="<td>"+item.itemCode+"</td>";
		html+="<td>"+item.chineseName+"</td>";
		html+="<td>"+item.articleBarCode+"</td>";
		html+="<td>"+item.brand+"</td>";
		html+="<td>"+item.warehouseStock+"("+item.realCurrentStock+")"+"</td>";
		html+="<td>"+item.purchaseSpecifications+item.salesUnit+"/"+item.purchasingUnit+"</td>";
		html+="<td><input type='text' class='form-control' data-purchaseSpecifications='"+item.purchaseSpecifications+"' onkeyup='limitInputNumber(this);countItem(this);' onchange='limitInputNumber(this);countItem(this);'/></td>";
		html+="<td><input type='text' class='form-control' name='itemNumber' value='0' readonly ></td>";
		html+="<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>";
		html+="<input type='hidden' name='itemIds' value='"+item.itemInformationID+"' /></td>";
		html+="</tr>";
		$("#purchaseRequestItemTbody").append(html);
	}
	
	function countItem(obj){
		var numInput=$(obj).parent().parent().find("input[name='itemNumber']");
		numInput.val("0");
		var num=$(obj).val()*$(obj).attr("data-purchaseSpecifications");
		numInput.val(num);
	}
	
	function deleteItem(obj){
		$(obj).parent().parent().remove();
	}
	
	function validateForm(){
		var length=$("#purchaseRequestItemTbody [name='itemIds']").length;
		if(!length>0){
			Prompt.alert("提示","物品种类数量必须大于0！")
			return false;
		}
		var arr=$("#purchaseRequestItemTbody [name='itemNumber']");
		for(var i=0;i<arr.length;i++){
			var num=$(arr[i]).val();
			if(num.trim()=='' || num<=0){
				Prompt.alert("提示","物品采购数量必须大于0！")
				return false;
			}
		}
		var deptID=$("#purchaseRequestDetailForm [name='deptID']").val();
		if(deptID==''){
			Prompt.alert("提示","请选择部门！")
			return false;
		}
		var cruiseID=$("#purchaseRequestDetailForm [name='cruiseID']").val();
		if(cruiseID==''){
			Prompt.alert("提示","请选择邮轮！")
			return false;
		}
		return true;
	}
	
	function saveAddPurchaseRequest(){
		if(!validateForm()) return ;
		$.ajax({
			url:"<%=basePath %>purchaseRequest/add.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#purchaseRequestDetailForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购申请保存成功！",function(){
						location.href="<%=basePath %>purchaseRequest/listPurchaseRequest.do";
					});
				}else{
					Prompt.alert("提示","采购申请保存失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购申请保存失败！");
			}
		});
	}
	//========================================================================
	/* 采购申请导入Excel */
	 $("#submitOrder").unbind();
	$("#submitOrder").on("click",function() {
		$("#submitOrderFile").click();
		$("#submitOrderFile").unbind();
		$("#submitOrderFile").on("change",function() {
			if($(this).val()){
				ajaxPuchaseOrderFile();
			}else{
				createPrompt("请选择Excel文件！");
			}
			
		});
		
	}); 
	function ajaxPuchaseOrderFile(){
		var formData = new FormData($("#submitOrderForm")[0]);
	    $.ajax({
	    	url: "<%=basePath %>purchaseRequest/excelInput",
	         type: "POST",
	         data: formData,
	         async: true,
	         contentType: false,
	         processData: false,
	        
	    });
	}
	</script>
</body>
</html>